package rad.framework.search;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

/**
 * @todo add comment for javadoc
 *
 * @author boudyacho
 */
@Entity
@Indexed(index="everything")
public class Book implements Serializable, Cloneable {
	
	private static final long serialVersionUID = 1L;

	public Book() {
	}

	public Book(String title, String author, String summary) {
		this.title = title;
		this.author = author;
		this.summary = summary;
		this.bind = Binding.HARDCOVER;
	}
	
	public Book(String title, String author, String summary, Binding bind) {
		this.title = title;
		this.author = author;
		this.summary = summary;
		this.bind = bind;
	}

	/**
	 * ------------------------------------------------------------------------
	 * The primary key.
	 * ------------------------------------------------------------------------
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@DocumentId
	public Long getId() {
		return id;
	}
	
	public void setId(final Long id) {
		this.id = id;
	}
	
	private Long id = null;

	/**
	 * ------------------------------------------------------------------------
	 * @todo add comment for javadoc
	 * ------------------------------------------------------------------------
	 */
	@Field(name = "author", store = Store.YES, index = Index.TOKENIZED)
	public String getAuthor() {
		return author;
	}

	public void setAuthor(final String author) {
		this.author = author;
	}

	private String author = null;

	/**
	 * ------------------------------------------------------------------------
	 * @todo add comment for javadoc
	 * ------------------------------------------------------------------------
	 */
	@Field(name = "title", store = Store.YES, index = Index.TOKENIZED)
	public String getTitle() {
		return title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	private String title = null;
	
	/**
	 * ------------------------------------------------------------------------
	 * @todo add comment for javadoc
	 * ------------------------------------------------------------------------
	 */
	@Field(name = "description", store = Store.YES, index = Index.TOKENIZED)
	public String getSummary() {
		return summary;
	}

	public void setSummary(final String summary) {
		this.summary = summary;
	}

	private String summary = null;
	
	/**
	 * ------------------------------------------------------------------------
	 * @todo add comment for javadoc
	 * ------------------------------------------------------------------------
	 */
	
	@Field(name="dabind", store=Store.YES, index = Index.TOKENIZED)
	public Binding getBind() {
		return bind;
	}
	
	public void setBind(final Binding bind){
		this.bind = bind;
	}
	
	private Binding bind;
	
	// ------------------------------------------------------------------------
	// Utils
	// ------------------------------------------------------------------------


	public String toString() {
		return ToStringBuilder.reflectionToString(this,ToStringStyle.MULTI_LINE_STYLE);
	}
	
	public Foo deepClone() throws Exception {
		Foo clone = (Foo) super.clone();
		clone.setId(null);

		return clone;
	}
	
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((id == null) ? super.hashCode() : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Foo))
			return false;
		final Foo other = (Foo) obj;
		if (id == null) {
			if (other.getId() != null)
				return false;
		} else if (!id.equals(other.getId()))
			return false;
		return true;
	}	
}